package cn.orange.ch10_dynamicprogramming;

/**
 * LC53.最大子数组和
 */
public class LC53 {
    public int maxSubArray(int[] nums) {
        int n = nums.length;
        //dp[i]:(到下标i结尾)数组的最大连续子数组和为dp[i]
        int[] dp = new int[n];
        dp[0] = nums[0];
        int ans = dp[0];
        for (int i = 1; i < n; i++) {
            dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
            if (dp[i] > ans) {
                ans = dp[i];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        LC53 alg = new LC53();
        System.out.println(alg.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
    }
}
